/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.info.service.plugins  
 */
package com.apache.info.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.rpc.manager.InterfaceRegisterManager;
import com.apache.tools.ClassToolsUtil;

/**
 * description:  接口注册action
 * @author Administrator 创建时间：2016-11-30  
 * @Copyright(c)2014:东吴（北京）软件技术有限公司
 */
public class ProtocolActionPluginImpl implements PluginConnector {

	private InterfaceRegisterManager interfaceRegisterManager;
	private IDao infoReleaseDao;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		String method = String.valueOf(param.getParams("method"));
		ResultEntity entity = new ResultEntity();
		entity.setResult("true");
		if ("loadCache".equalsIgnoreCase(method)) {
			loadCache(entity, param);
		} else if ("list".equalsIgnoreCase(method)) {
			list(entity, param);
		} else if ("save".equalsIgnoreCase(method)) {
			save(entity, param);
		} else if ("info".equalsIgnoreCase(method)) {
			info(entity, param);
		}
		param = null;
		return entity;
	}

	private void info(ResultEntity entity, ParamsVo param) {
		String registerId = String.valueOf(param.getParams("registerId"));
		MethodParam pm = new MethodParam("InterfaceRegister", "", "", "com.apache.info.entity.InfoRelease");
		pm.setParams("registerId", registerId);
		Object obj = infoReleaseDao.selectSingle(pm);
		entity.setEntity(obj);
		entity.setMessage("查询成功");
	}

	private void loadCache(ResultEntity entity, ParamsVo param) {
		ParamsVo<InterfaceRegister> vo = new ParamsVo<InterfaceRegister>();
		vo.setMethodKey("initCache");
		interfaceRegisterManager.execute(vo);
		entity.setEntity("true");
		entity.setMessage("操作成功！");
	}

	private void list(ResultEntity entity, ParamsVo param) {
		ParamsVo<InterfaceRegister> vo = new ParamsVo<InterfaceRegister>();
		String beanId = String.valueOf(param.getParams("beanId"));
		String beanName = String.valueOf(param.getParams("beanName"));
		String flag = String.valueOf(param.getParams("flag"));
		MethodParam pm = new MethodParam("InterfaceRegister", "", "", "com.apache.info.entity.InfoRelease");
		if (Validator.isNotNull(beanId)) {
			pm.setParams("beanId", beanId);
		}
		if (Validator.isNotNull(beanName)) {
			pm.setParams("beanName", beanName);
		}
		if (Validator.isNotNull(flag)) {
			pm.setParams("flag", flag);
		}
		String rows = String.valueOf(param.getParams("pageSize"));
		String page = String.valueOf(param.getParams("pageIndex"));
		pm.setPageSize(Integer.valueOf(Validator.getDefaultStr(rows, "20")));
		pm.setPageIndex(Integer.valueOf(Validator.getDefaultStr(page, "20")));

		Page pageList = infoReleaseDao.pageSelect(pm);
		entity.setEntity(pageList);
		entity.setMessage("查询成功");
	}

	private void save(ResultEntity entity, ParamsVo vo) {
		ParamsVo<InterfaceRegister> param = new ParamsVo<InterfaceRegister>();
		InterfaceRegister item = new InterfaceRegister();
		item = (InterfaceRegister) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), item);
		param.setObj(item);
		if (Validator.isNull(item.getRegisterId())) {
			item.setTimestampCreate(System.currentTimeMillis());
			item.setUserIdCreate("");
			String id = interfaceRegisterManager.saveInfo(param);
			if (Validator.isNull(id)) {
				entity.setMessage("保存失败");
			} else {
				entity.setEntity(id);
				entity.setMessage("保存成功");
			}
		} else {
			boolean mark = interfaceRegisterManager.editInfo(param);
			if (mark) {
				entity.setEntity(mark);
				entity.setMessage("修改成功");
			} else {
				entity.setMessage("修改失败");
			}
		}
	}

	public void setInterfaceRegisterManager(InterfaceRegisterManager interfaceRegisterManager) {
		this.interfaceRegisterManager = interfaceRegisterManager;
	}

	public void setInfoReleaseDao(IDao infoReleaseDao) {
		this.infoReleaseDao = infoReleaseDao;
	}

}
